package com.junzhiit.nengji.api.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.junzhiit.nengji.api.service.AddressService;
import com.junzhiit.nengji.api.web.exception.AddressNotFoundException;
import com.junzhiit.nengji.api.web.exception.UserNotFoundException;
import com.junzhiit.nengji.api.web.util.APICodeStatus;
import com.junzhiit.nengji.api.web.vo.AddressAddVO;
import com.junzhiit.nengji.api.web.vo.AddressEditVO;
import com.junzhiit.nengji.api.web.vo.AddressVO;
import com.junzhiit.nengji.api.web.vo.ResultVO;
import com.junzhiit.nengji.dao.AddressDAO;
import com.junzhiit.nengji.dao.UserDAO;
import com.junzhiit.nengji.model.Address;
import com.junzhiit.nengji.model.enums.Is;


@Service
public class AddressServiceImpl extends BaseServiceImpl implements AddressService{
	
	@Autowired
	private AddressDAO addressDAO;
	
	@Autowired
	private UserDAO userDAO;

	@Override
	public ResultVO findAll(String userId) {
		ResultVO result=new ResultVO();
		
		List<Address> list= addressDAO.findAll(userId);
		List<AddressVO> listVO = new ArrayList<AddressVO>();
		for (Address param : list) {
			AddressVO vo = new AddressVO();
			BeanUtils.copyProperties(param, vo);
			listVO.add(vo);
		}

		result.setData(listVO);
		return result;
	}

	@Override
	public ResultVO findById(String id) {
		ResultVO resultVO = new ResultVO();
		Address address = addressDAO.query(id);
		if (address == null) {
			throw new AddressNotFoundException("收货地址不存在");
		}
		AddressVO addressVO = new AddressVO();
		BeanUtils.copyProperties(address, addressVO);
		resultVO.setData(addressVO);
		return resultVO;
	}

	@Override
	@Transactional
	public ResultVO add(AddressAddVO addressAddVO) {
		Address address =new Address();
		BeanUtils.copyProperties(addressAddVO, address);
		ResultVO resultVO = new ResultVO();
		
		int count = addressDAO.countAddress(addressAddVO.getUserId());
		if(count > 0){
			if(addressAddVO.getIsChecked() == Is.YES.toIntValue()){
				//其余为非默认地址
				Address newAddress =new Address();
				newAddress.setUserId(addressAddVO.getUserId());
				newAddress.setIsChecked(Is.NO.toIntValue());
				addressDAO.updateStatus(newAddress);
			}
		}else{
			address.setIsChecked(Is.YES.toIntValue());
		}
		
		Address param =addressDAO.insert(address);
		AddressVO addressVO = new AddressVO();
		BeanUtils.copyProperties(param, addressVO);
		
		resultVO.setData(addressVO);
		return resultVO;
	}

	@Override
	@Transactional
	public ResultVO delete(String id,String userId) {
		ResultVO resultVO = new ResultVO();
		try {
			boolean is = addressDAO.updateToDelete(id);
			if(is){
				Address address = addressDAO.query(id);
				if(address != null && StringUtils.isNoneBlank(address.getId())){
					if(address.getIsChecked() == Is.YES.toIntValue()){
						int count =  addressDAO.countAddress(userId);
						if(count > 0){
							Address newAddress = addressDAO.getUserAddress(userId);
							if(newAddress != null){
								newAddress.setIsChecked(Is.YES.toIntValue());
								addressDAO.update(newAddress);
							}
						}
					}
				}
			}else{
				throw new AddressNotFoundException("收货地址不存在");
			}
		} catch (Exception e) {
			throw new AddressNotFoundException("收货地址不存在");
		}
		return resultVO;
	}

	@Override
	@Transactional
	public ResultVO edit(AddressEditVO addressEditVO) {
		ResultVO resultVO = new ResultVO();
		Address address =new Address();
		
		BeanUtils.copyProperties(addressEditVO, address);
		address.setUpdatedTime(new Date());
		
		if(addressEditVO.getIsChecked() == Is.YES.toIntValue()){
			//其余为非默认地址
			Address newAddress =new Address();
			newAddress.setUserId(addressEditVO.getUserId());
			newAddress.setIsChecked(Is.NO.toIntValue());
			addressDAO.updateStatus(newAddress);
		}
		resultVO.setData(addressDAO.update(address));
		return resultVO;
	}
	
	@Transactional
	public ResultVO updateStatus(String id, String userId) {
		ResultVO result = new ResultVO();
		int count = userDAO.checkMember(userId);
		if(count == 0)
			throw new UserNotFoundException("User not found.");
		
		Address address = addressDAO.getAddressById(id, userId);
		if(address!=null && StringUtils.isNotBlank(address.getId())){
			
			//其余为非默认地址
			address.setIsChecked(Is.NO.toIntValue());
			addressDAO.updateStatus(address);
			
			//修改为默认地址
			address.setIsChecked(Is.YES.toIntValue());
			addressDAO.updateById(address);
			
			result.setMessage("succeed.");
		}else{
			result.setStatus(APICodeStatus.ADDRESS_NOT_FOUND);
			result.setMessage("Address not found.");
			return result;
		}
		return result;
	}


}
